package com.example.zg.zg_312_ordering_new.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telecom.Call;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.adapter.FoodListAdapter;
import com.example.zg.zg_312_ordering_new.beans.CollectStatus;
import com.example.zg.zg_312_ordering_new.beans.FoodInfo;
import com.example.zg.zg_312_ordering_new.beans.Result;
import com.example.zg.zg_312_ordering_new.beans.Shop;
import com.example.zg.zg_312_ordering_new.common.Constants;
import com.example.zg.zg_312_ordering_new.fragments.HomeFragment;
import com.example.zg.zg_312_ordering_new.listener.RetrofitListener;
import com.example.zg.zg_312_ordering_new.model.FoodModel;

import java.util.List;

public class FoodListActivity extends BaseActivity implements RetrofitListener {

    private ImageView foodlist_return,foodlist_collect;
    private TextView foodlist_name;
    private RecyclerView recyclerView;
    int shop_id;
    private String phonenum,shopname;
    private Shop shop;
    String result;


    @Override
    void setLayout(int layout_file) {
        super.setLayout(R.layout.activity_foodlist);

    }

    @Override
    public void initViews() {
        recyclerView=findViewById(R.id.food_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        foodlist_return=(ImageView)findViewById(R.id.return1);
        foodlist_name=(TextView)findViewById(R.id.textView7);
        foodlist_collect = (ImageView)findViewById(R.id.imageView6);


    }

    @Override
    void initEvents() {
        foodlist_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        foodlist_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                collect();
            }
        });

    }

    @Override
    void initData() {

        Intent intent = getIntent();
        shop_id = intent.getIntExtra("shop_id",1);
        getFoodList();
        iscollected();



    }
    private void getFoodList(){
        FoodModel foodModel=new FoodModel();
        foodModel.getFoodByShop(shop_id,this);
    }
    private void iscollected(){
        FoodModel foodModel=new FoodModel();
        foodModel.isCollected(getUser_id(),shop_id,0,this);
    }
    private void collect(){
        FoodModel foodModel=new FoodModel();
        foodModel.userCollectShop(getUser_id(),shop_id,this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constants.GETFOODBYSHOP:
               FoodListAdapter adapter=new FoodListAdapter(FoodListActivity.this,(List<FoodInfo>)o,
                        R.layout.cardview_food,phonenum);
                recyclerView.setAdapter(adapter);
                break;
            case Constants.ISCOLLECTED:
                result = ((CollectStatus)o).getCollected().toString();
                showToast(((CollectStatus) o).getCollected());
//                Toast.makeText(FoodListActivity.this,result,Toast.LENGTH_SHORT).show();
                if (result.equals("0")){
                    foodlist_collect.setImageResource(R.drawable.uncollection);
                }else {
                    foodlist_collect.setImageResource(R.drawable.collection);
                }
            break;
            case Constants.USERCOLLECTFOOD:
             if (((Result)o).getSuccess().equals("1")){
                 if (result.equals("0")){
                     foodlist_collect.setImageResource(R.drawable.collection);
                     Toast.makeText(FoodListActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                     result="1";
                 }else {
                     foodlist_collect.setImageResource(R.drawable.uncollection);
                     Toast.makeText(FoodListActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                     result="0";
                 }
             }else {
                 Toast.makeText(FoodListActivity.this,"操作失败",Toast.LENGTH_SHORT).show();
             }
             break;
        }

    }


    @Override
    public void onFail() {
        showToast("网络连接错误");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        foodlist_name=findViewById(R.id.textView7);
        shop_id=getIntent().getIntExtra("id",0);
        shopname=getIntent().getStringExtra("shopname");
        foodlist_name.setText(shopname);

        FoodModel foodModel=new FoodModel();
        foodModel.getFoodByShop(shop_id,this);

    }
}

